# -*- coding: utf-8 -*-
# @Time: 2021/8/16 10:48
# @File: problem-61.py
# @DESC: https://leetcode-cn.com/problems/rotate-list/
# 旋转链表
from typing import Optional
from list_node_common import *


class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        if head == None:
            return head
        node_list = []
        cur = head
        while cur:
            node_list.append(ListNode(cur.val))
            cur = cur.next
        lent = len(node_list)
        nhead = ListNode(0)
        k = k % lent
        p = nhead
        for i in range(lent - k, lent):
            p.next = node_list[i]
            p = p.next
        for i in range(lent - k):
            p.next = node_list[i]
            p = p.next

        # zx = ListNode(0)
        return nhead.next


solution = Solution()
result = solution.rotateRight(listToNode([1, 2, 3, 4]), 4)
print()
